package com.example.portlocalforward.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.portlocalforward.controller.model.ApiResponse;
import com.example.portlocalforward.controller.model.TagRequest;
import com.example.portlocalforward.controller.model.TagResponse;
import com.example.portlocalforward.service.TagService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 标签控制器
 * 
 * @author Generated
 * @version 1.0.0
 */
@Api(tags = "标签管理", description = "标签管理接口")
@Slf4j
@RestController
@RequestMapping("/api/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    /**
     * 创建标签
     */
    @ApiOperation(value = "创建标签", notes = "创建新的标签")
    @PostMapping
    public ResponseEntity<ApiResponse<TagResponse>> createTag(
            @ApiParam(value = "标签信息", required = true) @RequestBody TagRequest tagRequest) {
        try {
            TagResponse response = tagService.createTag(tagRequest);
            return ResponseEntity.ok(ApiResponse.success("标签创建成功", response));
        } catch (RuntimeException e) {
            log.error("创建标签失败", e);
            return ResponseEntity.ok(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            log.error("创建标签失败", e);
            return ResponseEntity.ok(ApiResponse.error("创建标签失败: " + e.getMessage()));
        }
    }

    /**
     * 根据ID获取标签
     */
    @ApiOperation(value = "获取标签详情", notes = "根据ID获取标签详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<TagResponse>> getTag(
            @ApiParam(value = "标签ID", required = true) @PathVariable Long id) {
        try {
            Optional<TagResponse> tagOpt = tagService.getTagById(id);
            if (!tagOpt.isPresent()) {
                return ResponseEntity.ok(ApiResponse.error("标签不存在"));
            }
            return ResponseEntity.ok(ApiResponse.success(tagOpt.get()));
        } catch (Exception e) {
            log.error("获取标签失败", e);
            return ResponseEntity.ok(ApiResponse.error("获取标签失败: " + e.getMessage()));
        }
    }

    /**
     * 获取所有标签
     */
    @ApiOperation(value = "获取所有标签", notes = "获取所有标签列表")
    @GetMapping
    public ResponseEntity<ApiResponse<List<TagResponse>>> getAllTags() {
        try {
            List<TagResponse> tags = tagService.getAllTags();
            return ResponseEntity.ok(ApiResponse.success(tags));
        } catch (Exception e) {
            log.error("获取标签列表失败", e);
            return ResponseEntity.ok(ApiResponse.error("获取标签列表失败: " + e.getMessage()));
        }
    }

    /**
     * 更新标签
     */
    @ApiOperation(value = "更新标签", notes = "更新标签信息")
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<TagResponse>> updateTag(
            @ApiParam(value = "标签ID", required = true) @PathVariable Long id,
            @ApiParam(value = "标签信息", required = true) @RequestBody TagRequest tagRequest) {
        try {
            TagResponse response = tagService.updateTag(id, tagRequest);
            return ResponseEntity.ok(ApiResponse.success("标签更新成功", response));
        } catch (RuntimeException e) {
            log.error("更新标签失败", e);
            return ResponseEntity.ok(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            log.error("更新标签失败", e);
            return ResponseEntity.ok(ApiResponse.error("更新标签失败: " + e.getMessage()));
        }
    }

    /**
     * 删除标签
     */
    @ApiOperation(value = "删除标签", notes = "根据ID删除标签")
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteTag(
            @ApiParam(value = "标签ID", required = true) @PathVariable Long id) {
        try {
            tagService.deleteTag(id);
            return ResponseEntity.ok(ApiResponse.success("标签删除成功", null));
        } catch (RuntimeException e) {
            log.error("删除标签失败", e);
            return ResponseEntity.ok(ApiResponse.error(e.getMessage()));
        } catch (Exception e) {
            log.error("删除标签失败", e);
            return ResponseEntity.ok(ApiResponse.error("删除标签失败: " + e.getMessage()));
        }
    }
}

